package com.lagou.java.controller;

import com.lagou.java.pojo.User;
import com.lagou.java.pojo.User1;
import com.sun.net.httpserver.Authenticator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/demo")
public class DemoController {

//    @ExceptionHandler(ArithmeticException.class)
//    public void handleException (ArithmeticException exception, HttpServletResponse response) {
//        try {
//            response.getWriter().write(exception.getMessage());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    @RequestMapping("/handle01")
    public ModelAndView handle01 (@ModelAttribute("name") String name) {
        int i = 1/0;
        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date", date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/handle02")
    public String handle02 (ModelMap model) {
        Date date = new Date();
        model.addAttribute("date", date);
        return "success";
    }

    @RequestMapping("/handle03")
    public String handle03 (Model model) {
        Date date = new Date();
        model.addAttribute("date", date);
        return "success";
    }

    @RequestMapping("/handle04")
    public String handle04 (Map<String, Object> model) {
        Date date = new Date();
        model.put("date", date);
        return "success";
    }

    @RequestMapping("/handle05")
    public String handle04 (Date date) {
        System.out.println(date.toString());
        return "success";
    }

    @RequestMapping("/handle06/{id}")
    public void handle06 (@PathVariable("id")  String id) {
        System.out.println(id);
        return ;
    }

    @RequestMapping("/handle07")
    //不在走视图解析器
    @ResponseBody
    public User handle07 (@RequestParam String name, @RequestBody User user) {
        System.out.println(name);
        System.out.println(user);
        user.setName("张三丰");
        return user;
    }

    @RequestMapping("/upload")
    public String upload (MultipartFile uploadFile, HttpSession session) throws IOException {
        String originalFilename = uploadFile.getOriginalFilename();
        String newName = UUID.randomUUID() + "." + originalFilename.substring(originalFilename.lastIndexOf("."));

        String realPath = session.getServletContext().getRealPath("/uploads");
        String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        File folder = new File(realPath + "/" + datePath);

        if(!folder.exists()) {
            folder.mkdirs();
        }

        uploadFile.transferTo(new File(folder, newName));
        return "success";
    }

    @RequestMapping("/handleRedirect")
    public String handleRedirect (String name, RedirectAttributes redirectAttributes) {
        //设置了一个flash的属性, 该属性会被暂存到session中, 在跳转到页面之后, 该属性值消失
        redirectAttributes.addFlashAttribute("name", name);
        return "redirect:handle01";//拼接参数, 安全性, 参数长度都有限制
//        return "redirect:handle01?name=" + name;//拼接参数, 安全性, 参数长度都有限制
    }

    @RequestMapping("/users")
    public void users(User1 user) {
        System.out.println("haha");
    }


}
